<?php
require_once 'PHPUnit/Framework.php';
$dbms = "mysql";
$pathAdjust = "../../../scripts/";
if((@include_once "{$pathAdjust}initIncludePaths.inc") == "1")
    echo "included from $pathAdjust\r\n";
else {
    $pathAdjust = "../../scripts/";
    if((@include_once "{$pathAdjust}initIncludePaths.inc") == "1")
        echo "included from $pathAdjust\r\n";
    else {
        $pathAdjust = "../scripts/";
        if((@include_once "{$pathAdjust}initIncludePaths.inc") == "1")
            echo "included from $pathAdjust\r\n";
        else {
            $pathAdjust = "scripts/";
            if((@include_once "{$pathAdjust}initIncludePaths.inc") == "1")
                echo "included from $pathAdjust\r\n";
            else {
                $pathAdjust = "./";
                if((@include_once "{$pathAdjust}initIncludePaths.inc") == "1")
                    echo "included from $pathAdjust\r\n";
            }
        }
    }
}
$GLOBALS['adjPath'] = $pathAdjust;
include_once "dbFac.inc";
include_once "database.inc";
include_once "user.inc";


class UserTest extends PHPUnit_Framework_TestCase {
    private $user = null;
    private $db = null;

    protected function setUp() {
        $dbf = new DatabaseFactory("mysql","localhost","test2DB","root","");
        $this->db = $dbf->connect();
        $this->user = new User($this->db);
    }
    protected function tearDown()
    {
        unset($this->user);
        unset($this->db);
    }
    public function testConstruct() {
        //Empty user object
        $usr = new User($this->db);
        $this->assertEquals($usr->getID(),"");
        $this->assertEquals($usr->getUserObjectStatus(),"constructed");

        //User using all information
        $usr = new User($this->db,"Jens","n","Allmer","JA","jens@allmer.de","password","OK","Owner","http://www.allmer.de/jens");
        $this->assertEquals($usr->getID(),"-1");
        $this->assertEquals($usr->getfName(),"Jens");
        $this->assertEquals($usr->getmNames(),"n");
        $this->assertEquals($usr->getlName(),"Allmer");
        $this->assertEquals($usr->getInitials(),"JA");
        $this->assertEquals($usr->getEmail(),"jens@allmer.de");
        $this->assertEquals($usr->getPassword(),sha1("password"));
        $this->assertEquals($usr->getStatus(),"OK");
        $this->assertEquals($usr->getInfo(),"Owner");
        $this->assertEquals($usr->getWebPage(),"http://www.allmer.de/jens");
        $this->assertEquals($usr->getUserObjectStatus(),"filled");
        $this->db->executeSQL("DELETE FROM Users");
        $usr->createUser(null);     //Create a user without a group

        //use the newly created user to test the remaining constructors
        //create user by id
        $usr1 = new User($this->db,$usr->getID());
        $this->assertEquals($usr1->getID(),$usr->getID());
        $this->assertEquals($usr1->getfName(),$usr->getfName());
        $this->assertEquals($usr1->getmNames(),$usr->getmNames());
        $this->assertEquals($usr1->getlName(),$usr->getlName());
        $this->assertEquals($usr1->getInitials(),$usr->getInitials());
        $this->assertEquals($usr1->getEmail(),$usr->getEmail());
        $this->assertEquals($usr1->getPassword(),$usr->getPassword());
        $this->assertEquals($usr1->getStatus(),$usr->getStatus());
        $this->assertEquals($usr1->getInfo(),$usr->getInfo());
        $this->assertEquals($usr1->getWebPage(),$usr->getWebPage());
        $this->assertEquals($usr1->getUserObjectStatus(),"filled");

        //create user by email and password (unencrypted)
        $usr2 = new User($this->db,$usr->getEmail(),"password");
        $this->assertEquals($usr2->getID(),$usr->getID());
        $this->assertEquals($usr2->getfName(),$usr->getfName());
        $this->assertEquals($usr2->getmNames(),$usr->getmNames());
        $this->assertEquals($usr2->getlName(),$usr->getlName());
        $this->assertEquals($usr2->getInitials(),$usr->getInitials());
        $this->assertEquals($usr2->getEmail(),$usr->getEmail());
        $this->assertEquals($usr2->getPassword(),$usr->getPassword());
        $this->assertEquals($usr2->getStatus(),$usr->getStatus());
        $this->assertEquals($usr2->getInfo(),$usr->getInfo());
        $this->assertEquals($usr2->getWebPage(),$usr->getWebPage());
        $this->assertEquals($usr2->getUserObjectStatus(),"filled");

        //create user by email and password (encrypted)
        $usr2 = new User($this->db,$usr->getEmail(),$usr->getPassword());
        $this->assertEquals($usr2->getID(),$usr->getID());
        $this->assertEquals($usr2->getfName(),$usr->getfName());
        $this->assertEquals($usr2->getmNames(),$usr->getmNames());
        $this->assertEquals($usr2->getlName(),$usr->getlName());
        $this->assertEquals($usr2->getInitials(),$usr->getInitials());
        $this->assertEquals($usr2->getEmail(),$usr->getEmail());
        $this->assertEquals($usr2->getPassword(),$usr->getPassword());
        $this->assertEquals($usr2->getStatus(),$usr->getStatus());
        $this->assertEquals($usr2->getInfo(),$usr->getInfo());
        $this->assertEquals($usr2->getWebPage(),$usr->getWebPage());
        $this->assertEquals($usr2->getUserObjectStatus(),"filled");
    }

    public function testChangePassword() {

    }
}
?>
